package org.jeecg.modules.website;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class HkInterfaceUtil {

    private static final String APP_USERNAME = "15038051279";
    private static final String APP_PASSWORD = "Duhaikuan1279";
    private static final String APP_KEY = "1896741740589899827";
    private static final String APP_SECRET = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMXgAANqphBCM+Kznu8154/6OxPl9+qbQJ7N2f5go+u6oYl4ajwwm+5S4+EiaZHgF8Dty2D/Mh/ch2CaZ//AKIoyrD98DiyzvMnzYF9oKOddgN9DMtxi7Ru0E/Hq9mpiRnUGdKfiZnxiLR0CGIzAGUHi/RhTLztJ2ndJRegLf+71AgMBAAECgYAFt/GIJ77g3Wjudk5zj1U/3fVzH8RIbXAyprRUG6rhFyWv+WDomGcJPjADw6DVbGqDUtv706Q5M4BeFFhJFd6WAsVEMLEqyG5eoSvWrV7HIxaCPZT5TSM6LD5bbj4sMCg5WHtfeA2qFl+lsyOtFQJyci4u5BHOXge3LbbNw+Q2CQJBAPwCO2BkLcT+++X6ciK7+FFJemNejnNMkJXDDrmTaoMRxZkPj1iyqNm9MU1UajNW4om3IwuCiAjRLhjs0QQlcz8CQQDJAkiBqhEZTB/A1Q1G2c6gZ1Uu3v+Gqy3tUKlDAvmcG+HiFEZM/vVRmLrw3PmjQpTJ6iwfTTcyKDXNaRwSK3TLAkBDIokU5bs3tsH+g1iynibJ6Ow82qzSVHxUGVGr6HTAlBRy8S078OSAlSE0O5/LK4NQMsSYRV7eEnPYWgZYt21bAkAwe1N/qsRLBLhX60szfo8+0w2Rq4mYMqt2JNFeb1ZDF9qIi/lU0t3am2rjc3jmrsVyWf/gYOBiQZ6Qd7ykiUS3AkEA4pCiJ/eKZQVAcca9eSrBnrjbGzP7gHRv3LXzI/wZQ/X5v9IEFqjEhe1/fH7rx+aAdak8sWIJ6RdJG7F6PUbVzw==";
    private static final String REDICT_URL = "https://www.sojson.com/http/test.html";

    // 获取应用token接口
    public static String getAppAccessToken(){
        String url = "https://open-api.hikiot.com/auth/exchangeAppToken";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appKey", APP_KEY);
        jsonObject.put("appSecret", APP_SECRET);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");

        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jsonObject.toJSONString()).timeout(30*1000).execute().body();

        log.info("获取应用token返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);
        if(dataobj.getString("code").equals("0")){
            JSONObject obj = dataobj.getJSONObject("data");
            return obj.getString("appAccessToken");
        }
        return null;
    }

    // 申请授权码
    public static String applyAuthCode(){
        String url = "https://open-api.hikiot.com/auth/third/applyAuthCode";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appKey", APP_KEY);
        jsonObject.put("userName", APP_USERNAME);
        jsonObject.put("password", APP_PASSWORD);
        jsonObject.put("redirectUrl", REDICT_URL);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");

        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jsonObject.toJSONString()).timeout(30*1000).execute().body();

        log.info("申请授权码接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);
        if(dataobj.getString("code").equals("0")){
            JSONObject obj = dataobj.getJSONObject("data");
            return obj.getString("authCode");
        }
        return null;
    }

    // 获取用户token
    public static String getUserAccessToken(){
        String url = "https://open-api.hikiot.com/auth/third/code2Token";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("authCode", "fa6d5c2c09a726c51dd7c2ea1b31a330");

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("App-Access-Token", "at-_CuNEOT3UAa7N8JiCg9QqW3MkSqi_GIdD9solZWe");

        String result = HttpRequest.get(url)
                .headerMap(heads, false)
                .body(jsonObject.toJSONString()).timeout(30*1000).execute().body();

        log.info("申请授权码接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);
        if(dataobj.getString("code").equals("0")){
            JSONObject obj = dataobj.getJSONObject("data");
            return obj.getString("userAccessToken");
        }
        return null;
    }


    /**
     * 添加人员
     * @return
     */
    public static void  addPerson(){
        String url = "https://open-api.hikiot.com/device/direct/v1/userInfo/addOneRecord";

        JSONObject jsonObject = new JSONObject();
        JSONObject userInfo = new JSONObject();
        userInfo.put("employeeNo", "1"); //员工编号
        userInfo.put("name", "2");  //姓名
        userInfo.put("userType", "normal");
        userInfo.put("enableBeginTime", "2021-11-25T00:00:00");
        userInfo.put("enableEndTime", "2035-11-25T00:00:00");
        userInfo.put("doorRightPlan", null);  //不超过门数量
        JSONObject payload = new JSONObject();
        payload.put("userInfo",userInfo );
        jsonObject.put("deviceSerial", "2222");   // 设备序列号
        jsonObject.put("payload", payload);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("App-Access-Token", "");
        heads.put("User-Access-Token", "");

        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jsonObject.toJSONString()).timeout(30*1000).execute().body();

        log.info("人员添加接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);

    }

    /**
     * 添加人员(通讯录)
     * @return
     */
    public static String  addPersonNew() throws Exception {
        String url = "https://open-api.hikiot.com/team/v1/person/add";

        JSONObject userInfo = new JSONObject();
        userInfo.put("personName", "陈鹏"); //员工编号
        userInfo.put("idCard", "");
        userInfo.put("departNo", "BM19584586");  //姓名
        userInfo.put("jobNumber", "");
        userInfo.put("jobPosition", "");
        userInfo.put("phone", "17630716689");
        userInfo.put("sex", "");

        JSONObject jmBody = new JSONObject();
        jmBody.put("bodySecret", RSAUtils.encryptByPrivateKey(userInfo.toJSONString(),APP_SECRET));

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("App-Access-Token", "at-G1oiS92ZOXlOceKgNQ6IT0xmbPJiGSlR8qzSjdgt");
        heads.put("User-Access-Token", "ut-63c47b4e-d445-4a44-a874-fcf509e2a7e7");

        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jmBody.toJSONString()).timeout(30*1000).execute().body();

        log.info("人员添加接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);
        if(dataobj.getString("code").equals("0")){
            JSONObject obj = dataobj.getJSONObject("data");
            return obj.getString("personNo");
        }
        return null;

    }


    /**
     * 删除人员(通讯录)
     * @return
     */
    public static void  delPersonNew(){
        String url = "https://open-api.hikiot.com/team/v1/person/removeByNo";

        JSONObject userInfo = new JSONObject();
        userInfo.put("personNo", ""); //员工编号

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("App-Access-Token", "");
        heads.put("User-Access-Token", "");

        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(userInfo.toJSONString()).timeout(30*1000).execute().body();

        log.info("人员添加接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);

    }

    /**
     * 添加人脸（生物信息）
     * @return
     */
    public static void  addFaceNew() throws Exception {
        String url = "https://open-api.hikiot.com/team/v1/person/addIdentification";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("personNo", "CY028801481");
        jsonObject.put("type", 3);
        jsonObject.put("content", "http://118.190.201.9:5000/ggzyiypt/sys/common/static/a94478f686615ede9118bbae80bf1cd_1741930008376.jpg");

        JSONObject jmBody = new JSONObject();
        jmBody.put("bodySecret", RSAUtils.encryptByPrivateKey(jsonObject.toJSONString(),APP_SECRET));

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json");
        heads.put("App-Access-Token", "at-G1oiS92ZOXlOceKgNQ6IT0xmbPJiGSlR8qzSjdgt");
        heads.put("User-Access-Token", "ut-63c47b4e-d445-4a44-a874-fcf509e2a7e7");

        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jmBody.toJSONString()).timeout(30*1000).execute().body();

        log.info("人员添加接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);

    }

    /**
     * 删除人脸（生物信息）
     * @return
     */
    public static void  delFaceNew(){
        String url = "https://open-api.hikiot.com/team/v1/person/deleteIdentification";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", "");


        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("App-Access-Token", "");
        heads.put("User-Access-Token", "");

        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jsonObject.toJSONString()).timeout(30*1000).execute().body();

        log.info("人员添加接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);

    }

    /**
     * 查询人脸（生物信息）
     * @return
     */
    public static void  queryFaceNew(){
        String url = "https://open-api.hikiot.com/team/v1/person/listIdentifications";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("personNo", "");


        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("App-Access-Token", "");

        String result = HttpRequest.get(url)
                .headerMap(heads, false)
                .body(jsonObject.toJSONString()).timeout(30*1000).execute().body();

        log.info("人员添加接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);

    }

    /**
     * 添加人脸
     * @return
     */
    public static String  addFace(){
        String url = "https://open-api.hikiot.com/device/direct/v1/faceAccess/addOneRecord";

        JSONObject jsonObject = new JSONObject();
        JSONObject faceInfo = new JSONObject();
        faceInfo.put("employeeNo", "1"); //员工编号
        faceInfo.put("faceURL", "2");  //姓名
        faceInfo.put("faceLibType", "blackFD");
        faceInfo.put("faceDbId", "1");
        JSONObject payload = new JSONObject();
        payload.put("faceInfo",faceInfo );
        jsonObject.put("deviceSerial", "2222");   // 设备序列号
        jsonObject.put("payload", payload);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("App-Access-Token", "");
        heads.put("User-Access-Token", "");

        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jsonObject.toJSONString()).timeout(30*1000).execute().body();

        log.info("添加人脸接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);
        if(dataobj.getString("code").equals("0")){
            JSONObject obj = dataobj.getJSONObject("data");
            return obj.getString("traceId");
        }
        return null;

    }


    /**
     * 删除人脸
     * @return
     */
    public static String  delFace(){
        String url = "https://open-api.hikiot.com/device/direct/v1/faceAccess/deleteByKey";

        JSONObject jsonObject = new JSONObject();
        JSONObject faceInfo = new JSONObject();
        faceInfo.put("employeeNo", "1"); //员工编号
        faceInfo.put("faceLibType", "blackFD");
        faceInfo.put("faceDbId", "1");
        JSONObject payload = new JSONObject();
        payload.put("faceInfo",faceInfo );
        jsonObject.put("deviceSerial", "2222");   // 设备序列号
        jsonObject.put("payload", payload);

        Map<String, String > heads = new HashMap<>();
        heads.put("Content-Type", "application/json;charset=UTF-8");
        heads.put("App-Access-Token", "");
        heads.put("User-Access-Token", "");

        String result = HttpRequest.post(url)
                .headerMap(heads, false)
                .body(jsonObject.toJSONString()).timeout(30*1000).execute().body();

        log.info("删除人脸接口返回："+result);
        JSONObject dataobj = JSONObject.parseObject(result);
        if(dataobj.getString("code").equals("0")){
            JSONObject obj = dataobj.getJSONObject("data");
            return obj.getString("traceId");
        }
        return null;

    }

    public static void main(String[] args) throws Exception {
        addFaceNew();
    }

}
